import { Injectable, OnModuleInit } from "@nestjs/common";
import { Cron, Interval } from "@nestjs/schedule";
import { GlobalStatusService } from "../global/global-status/global-status.service";

const si = require("systeminformation");

@Injectable()
export class ScheduleService implements OnModuleInit {
  constructor(private readonly globalStatusService: GlobalStatusService) {}

  @Interval(10 * 1000)
  async handleCron() {
    const stats = await si.fsSize();
    const ans = stats.filter((item) => ["C:", "D:"].includes(item.fs));
    const map = {
      "C:": "localC",
      "D:": "localD",
    };
    let diskUsage = {};
    ans.map((disk) => {
      diskUsage[map[disk.fs]] = {
        used: disk.used,
        total: disk.size,
        usePercent: disk.use,
      };
    });
    this.globalStatusService.updateDiskUsage(diskUsage);
  }

  onModuleInit(): any {
    this.handleCron();
  }
}
